<?php
 include 'include/setup.php';

$set = new setup();

if(!isset($_SESSION['user_login'])) { // έλεγχος αν έχει συνδεθεί ο χρήστης
    $set->redirect("http://192.185.116.17/~wwwmsc/administrator/index.php");
}
//if($_SESSION['userlevel'] != 1) {
//    $set->redirect("http://192.185.116.17/~wwwmsc/administrator/index.php");
//}

include 'include/head.php';
include 'include/menu.php';

    //ερωτήματα στην βάση για τον αριθμό χρηστών, εταιριών και διαφημίσεων
    $users_total_result = $set->qry("Select * from USERS WHERE user_level = 4");
    $users_day_result = $set->qry("select * from USERS WHERE user_level = 4 AND user_date >= (now() - interval 1 day)");
    $users_week_result = $set->qry("select * from USERS WHERE user_level = 4 AND user_date >= (now() - interval 7 day)");
    $users_month_result = $set->qry("select * from USERS WHERE user_level = 4 AND user_date >= (now() - interval 30 day)");

    $companies_total_results = $set->qry("Select * from USERS WHERE user_level = 3");
    $companies_day_result = $set->qry("select * from USERS WHERE user_level = 3 AND user_date >= (now() - interval 1 day)");
    $companies_week_result = $set->qry("select * from USERS WHERE user_level = 3 AND user_date >= (now() - interval 7 day)");
    $companies_month_result = $set->qry("select * from USERS WHERE user_level = 3 AND user_date >= (now() - interval 30 day)");

    $advs_total_results = $set->qry("Select * from ADS");
    $advs_day_results = $set->qry("Select * from ADS where ad_date >= (now() - interval 1 day)");
    $advs_week_results = $set->qry("Select * from ADS where ad_date >= (now() - interval 7 day)");
    $advs_month_results = $set->qry("Select * from ADS where ad_date >= (now() - interval 30 day)");


    $users_total_result = mysql_num_rows($users_total_result);
    $users_day_result = mysql_num_rows($users_day_result);
    $users_week_result = mysql_num_rows($users_week_result);
    $users_month_result = mysql_num_rows($users_month_result);

    $companies_total_results = mysql_num_rows($companies_total_results);
    $companies_day_result = mysql_num_rows($companies_day_result);
    $companies_week_result = mysql_num_rows($companies_week_result);
    $companies_month_result = mysql_num_rows($companies_month_result);

    $advs_total_results = mysql_num_rows($advs_total_results);
    $advs_day_results = mysql_num_rows($advs_day_results);
    $advs_week_results = mysql_num_rows($advs_week_results);
    $advs_month_results = mysql_num_rows($advs_month_results);

?>

<head>
    <link href="css/admin_style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {// γραφήματα
            $('#statistics_chart').highcharts({
                chart: {
                    type: 'column'
                },
                title: {
                    text: 'NewCareer'
                },
                subtitle: {
                    text: ''
                },
                xAxis: {
                    categories: [
                        'Users',
                        'Companies',
                        'Advertisments'
                    ]
                },
                yAxis: {
                    min: 0,
                    title: {
                        text: ''
                    }
                },
                exporting: {
                    enabled : false
                },

                credits: {
                    enabled: false
                },
                legend: {
                    enabled: false
                },
                tooltip: {
                    headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                    pointFormat: '<tr><td style= "border: none; color:{series.color};padding:0">{series.name}: </td>' +
                        '<td style= "border: none; padding:0"><b>{point.y:.1f}</b></td></tr>',
                    footerFormat: '</table>',
                    shared: true,
                    useHTML: true
                },
                plotOptions: {
                    column: {
                        pointPadding: 0.2,
                        borderWidth: 0
                    }
                },
                series: [{
                    name: 'Total',
                    data: [<?php echo $users_total_result?> , <?php echo $companies_total_results?> , <?php echo $advs_total_results?>]

                }, {
                    name: 'Day',
                    data: [<?php echo $users_day_result?> , <?php echo $companies_day_result?> , <?php echo $advs_day_results?>]

                }, {
                    name: 'Week',
                    data: [<?php echo $users_week_result?> , <?php echo $companies_week_result?> , <?php echo $advs_week_results?>]

                }, {
                    name: 'Month',
                    data: [<?php echo $users_month_result?> , <?php echo $companies_month_result?> , <?php echo $advs_month_results?>]

                }]
            });
        });


    </script>
</head>

<body class="background">

<div id = hometablediv>
    <table id = hometable>
        <th style="width: 33%"><h3>Users</h3></th><th style="width: 33%"><h3>Companies</h3></th><th style="width: 33%"><h3>Advertisements</h3></th>
        <tr>
            <td><?php echo 'Total users: '.($users_total_result)?></td>
            <td><?php echo 'Total companies: '.($companies_total_results)?></td>
            <td><?php echo 'Total Advertisements: '.($advs_total_results)?></td>
        </tr>
        <tr>
            <td><?php echo 'Users today: '.($users_day_result)?></td>
            <td><?php echo 'Companies Today: '.($companies_day_result)?></td>
            <td><?php echo 'Advertisements Today: '.($advs_day_results)?></td>
        </tr>
        <tr>
            <td><?php echo 'Users last week: '.($users_week_result)?></td>
            <td><?php echo 'Companies last week: '.($companies_week_result)?></td>
            <td><?php echo 'Advertisements last week: '.($advs_week_results)?></td>
        </tr>
        <tr>
            <td><?php echo 'Users last month: '.($users_month_result)?></td>
            <td><?php echo 'Companies last month: '.($companies_month_result)?></td>
            <td><?php echo 'Advertisements last month: '.($advs_month_results)?></td>
        </tr>

    </table>
</div>

<script src="js/highcharts.js"></script>
<script src="js/exporting.js"></script>

<div id="statistics_chart" style="width: 85%; height: 400px; margin: 0 auto; margin-top: 50px"></div>

</body>